﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;

using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace ConsolidationTool.WebParts.Layouts.CReport.Brochure
{
    public partial class P11QS : FSReportBase
    {

        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override Repeater BaseGAP
        {
            get
            {
                return RPGAP;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            base.GenerateReport_Click(sender, e);
              

            BindLastMonth(TopSelector.drpEntity.SelectedValue, TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text);
        }

        public override void ExportReport_Click(object sender, EventArgs e)
        {
            P6PL colData = null;
            colData = new P6PL();
            colData.row01 = Label02_Day02.Text;
            colData.row02 = Label03_Day02.Text;
            colData.row03 = Label05_Day02.Text;
            colData.row04 = Label06_Day02.Text;
            colData.row05 = Label02_Day03.Text;
            colData.row06 = Label03_Day03.Text;
            colData.row07 = Label05_Day03.Text;
            colData.row08 = Label06_Day03.Text;
            colData.row09 = Label02_Day04.Text;
            colData.row10 = Label03_Day04.Text;
            colData.row11 = Label05_Day04.Text;
            colData.row12 = Label06_Day04.Text;
            colData.row13 = Label01_Day06.Text;
            colData.row14 = Label02_Day06.Text;
            colData.row15 = Label03_Day06.Text;
            colData.row16 = Label01_Day07.Text;
            colData.row17 = Label02_Day07.Text;
            colData.row18 = Label03_Day07.Text;

            var RQReportDataSource = ViewState["ART_GAP_DataSource"] as List<RQDataItem>;

            colData.repObj = RQReportDataSource;

            string fileName = TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text + Constants._Brochure_P11_PRIFIX + TopSelector.drpEntity.SelectedValue + ".xlsx";
            BusinessService.CreateP11Excel(fileName, lbCM.Text, lbYTD.Text, colData);
            SPSite site = new SPSite(SPContext.Current.Site.ID);
            string reportUrl = site.Url + "/" + Constants._EXCEL_Library + "/" + fileName;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", string.Format("<script>window.open('" + reportUrl + "');</script>"));

        }


        void BindLastMonth(string coCd, string year, string month)
        {
            Label01_Day01.Text = "Accidents - Roquette";
            Label02_Day01.Text = "Declared";
            Label03_Day01.Text = "With lost time";
            Label04_Day01.Text = "Accidents- External Labour";
            Label05_Day01.Text = "Declared";
            Label06_Day01.Text = "With lost time";

            Label01_Day05.Text = "TG";
            Label02_Day05.Text = "TF1";
            Label03_Day05.Text = "TF2";

            int y = Convert.ToInt32(year) - 1;
            string yearN_1 = Convert.ToString(y);
            SPListItem item = Common.BusinessService.GetItem(coCd, year, month, "Quality&Safety");
            SPListItem itemN_1 = Common.BusinessService.GetItem(coCd, yearN_1, month, "Quality&Safety");

            if (item != null)
            {
                Label02_Day02.Text = item["QS01010101"].ToString();
                Label03_Day02.Text = item["QS01010102"].ToString();
                Label05_Day02.Text = item["QS01010201"].ToString();
                Label06_Day02.Text = item["QS01010202"].ToString();

                Label01_Day07.Text = item["QS01010401"].ToString();
                Label02_Day07.Text = item["QS01010402"].ToString();
                Label03_Day07.Text = item["QS01010403"].ToString();
            }
            else
            {
                Label02_Day02.Text = "0";
                Label03_Day02.Text = "0";
                Label05_Day02.Text = "0";
                Label06_Day02.Text = "0";

                Label01_Day07.Text = "0";
                Label02_Day07.Text = "0";
                Label03_Day07.Text = "0";
            }

            if (itemN_1 != null)
            {
                Label01_Day06.Text = itemN_1["QS01010401"].ToString();
                Label02_Day06.Text = itemN_1["QS01010402"].ToString();
                Label03_Day06.Text = itemN_1["QS01010403"].ToString();
            }
            else
            {
                Label01_Day06.Text = "0";
                Label02_Day06.Text = "0";
                Label03_Day06.Text = "0";
            }

            int m = Convert.ToInt32(month);

            decimal currentC01 = 0;
            decimal currentC02 = 0;
            decimal currentC03 = 0;
            decimal currentC04 = 0;
            decimal currentN01 = 0;
            decimal currentN02 = 0;
            decimal currentN03 = 0;
            decimal currentN04 = 0;

            for (int i = 1; i <= m; i++)
            {
                string strMonth = i.ToString();
                SPListItem itemCurrent = Common.BusinessService.GetItem(coCd, year, strMonth, "Quality&Safety");
                SPListItem itemN = Common.BusinessService.GetItem(coCd, yearN_1, strMonth, "Quality&Safety");

                if (itemCurrent != null)
                {
                    currentC01 = currentC01 + Convert.ToDecimal(itemCurrent["QS01010101"].ToString());
                    currentC02 = currentC02 + Convert.ToDecimal(itemCurrent["QS01010102"].ToString());
                    currentC03 = currentC03 + Convert.ToDecimal(itemCurrent["QS01010201"].ToString());
                    currentC04 = currentC04 + Convert.ToDecimal(itemCurrent["QS01010202"].ToString());
                }
                if (itemN != null)
                {
                    currentN01 = currentN01 + Convert.ToDecimal(itemN["QS01010101"].ToString());
                    currentN02 = currentN02 + Convert.ToDecimal(itemN["QS01010102"].ToString());
                    currentN03 = currentN03 + Convert.ToDecimal(itemN["QS01010201"].ToString());
                    currentN04 = currentN04 + Convert.ToDecimal(itemN["QS01010202"].ToString());
                }
            }
            Label02_Day03.Text = Convert.ToString(currentN01);
            Label03_Day03.Text = Convert.ToString(currentN02);
            Label05_Day03.Text = Convert.ToString(currentN03);
            Label06_Day03.Text = Convert.ToString(currentN04);

            Label02_Day04.Text = Convert.ToString(currentC01);
            Label03_Day04.Text = Convert.ToString(currentC02);
            Label05_Day04.Text = Convert.ToString(currentC03);
            Label06_Day04.Text = Convert.ToString(currentC04);
        }    

    }
}
